#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main() {
    int n;
    cin >> n;
    int ans = 1000000001;
    string str1, str2;
    int pre1 = -1, pre2 = -1;
    vector<string> strs(n);

    cin >> str1 >> str2;
    for (int i = 0; i < n; i++) {
        cin >> strs[i];
        if (strs[i] == str1)
        {
            pre1 = i;
            if (pre2 != -1)
            {
                ans = min(ans, pre1 - pre2);
            }
        }
        else if (strs[i] == str2)
        {
            pre2 = i;
            if (pre1 != -1)
            {
                ans = min(ans, pre2 - pre1);
            }
        }
    }

    if (pre1 == -1 || pre2 == -1)cout << -1 << endl;
    else cout << ans << endl;
    return 0;
}